package net.atomtree.util;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.runner.Describable;

public class HBaseTableManage {
	
	/*
	 * TableList: �г����б�
	 * getTableStatus: �����״̬disable/enable ,falseΪdisable״̬��  trueΪenable״̬
	 * createNewTable: ��Ĵ���
	 * ������ߣ�����
	 * ɾ����
	 * 
	 * ��ձ�
	 */
	
	
	
	/*
	 * ���ؼ�Ⱥ�����еı���б���
	 * ���صı����enable��disable״̬�ı�
	 */
	public ArrayList<String>  TableList(Connection conn) throws IOException{
		Admin hadmin = null;
		ArrayList<String> list = new ArrayList<String>();
		hadmin = conn.getAdmin();
		HTableDescriptor[] tablesdesc = hadmin.listTables();
		String tablename = null;
		for (HTableDescriptor t : tablesdesc) {
			tablename = t.getTableName().toString();
			list.add(tablename);
		}
		return list ;
	}
	
	
	/*
	 * ���ر������״̬��falseΪdisable״̬��  trueΪenable״̬
	 */
	
	public  boolean getTableStatus(Connection conn, String tablename) throws IOException{
		boolean statuscode ;
		Admin hadmin = conn.getAdmin();
		statuscode = hadmin.isTableEnabled(TableName.valueOf(tablename));
		return statuscode;
	}
	
	
	
	
	/*
	 * HBase�д����±�ָ����������������
	 */
	public boolean createNewTable(Connection conn, String tablename,String colfamily) throws IOException	{
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename));
		desc.addFamily(new HColumnDescriptor(colfamily));
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			System.out.println("************��ǰ���Ѿ�����************");
			return false;
		}	
		hadmin.createTable(desc);
		System.out.println("************�����ɹ�************");
		return true;
	}
	
	
	/*
	 * HBase�д����±�ָ����������������
	 */
	public boolean createNewTable(Connection conn, String tablename,String colfamily ,String colfamily1) throws IOException	{
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename));
		desc.addFamily(new HColumnDescriptor(colfamily));
		desc.addFamily(new HColumnDescriptor(colfamily1));
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			System.out.println("************��ǰ���Ѿ�����************");
			return false;
		}	
		hadmin.createTable(desc);
		System.out.println("************�����ɹ�************");
		return true;
	}
	
	
	/*
	 * HBase�д����±�ָ���������������壬���ƶ�Ԥ������ɢ������
	 * ���磺
	 * byte[][] regions = new byte[][] {   
				  Bytes.toBytes("3101200"),    
				  Bytes.toBytes("31012011"),
				  Bytes.toBytes("31012012")};
	 */
	public boolean createNewTable(Connection conn, String tablename,String colfamily ,byte[][] regionslist) throws IOException{
		
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename));
		desc.addFamily(new HColumnDescriptor(colfamily));
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			System.out.println("************��ǰ���Ѿ�����************");
			return false;
		}	
		hadmin.createTable(desc, regionslist);
		System.out.println("********������ɹ�*********");
		return true;
	}
	
	
	/*
	 * �����ߣ�ִ��disable����
	 */
	
	public void disableTable(Connection conn , String tablename) throws IOException {
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			hadmin.disableTable(TableName.valueOf(tablename));
		}
		
	}
	
	
	/*
	 * ������
	 */
	
	public void enableTable(Connection conn ,String tablename) throws IOException{
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			hadmin.enableTable(TableName.valueOf(tablename));
		}
		
	}
	/*
	 * ɾ����
	 */
	
	public void dropTable(Connection conn , String tablename) throws IOException {
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		if(hadmin.tableExists(TableName.valueOf(tablename))){
			hadmin.deleteTable(TableName.valueOf(tablename));
		}
		
	}
	
	/*
	 * ������ݱ�
	 */
	public void  truncateTable(Connection conn ,String tablename) throws IOException{
		Admin hadmin = null;
		hadmin = conn.getAdmin();
		hadmin.truncateTable(TableName.valueOf(tablename), false);
	}
	
	
	
	public static void main(String[] args) throws IOException{
		CreateConnection hc = new CreateConnection();
		Connection conn = hc.getConn();
		
		HBaseTableManage ht = new HBaseTableManage();
		ArrayList<String>  list = ht.TableList(conn);
		for (String s : list){
			System.out.println(s);
			System.out.println("��״̬"+ht.getTableStatus(conn, s));
		}	
	}

}
